﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace QuantumFractals
{
    public partial class PropertiesForm : Form
    {
        public string FractalName
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }
        public string Author
        {
            get { return txtAuthor.Text; }
            set { txtAuthor.Text = value; }
        }
        public string CreationDate
        {
            get { return txtCreationDate.Text; }
            set { txtCreationDate.Text = value; }
        }
        public string FormulaAlpha
        {
            get { return txtFormulaAlpha.Text; }
            set { txtFormulaAlpha.Text = value; }
        }
        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }
        public string[] FormulaDetectors
        {
            get { return txtFormulaDetectors.Lines; }
            set { txtFormulaDetectors.Lines = value; }
        }
        public Dictionary<string, string> FormulaConstants
        {
            get 
            {
                Dictionary<string, string> constants = new Dictionary<string, string>();
                foreach (string line in txtFormulaConstants.Lines)
                {
                    if (line.Trim() != "")
                    {
                        string[] s = line.Split(new char[] { '=' }, 2);
                        if (s.Length != 2)
                            throw new Exception(String.Format("Bad constant in formula: {0}", line));
                        constants.Add(s[0], s[1]);
                    }
                }
                return constants; 
            }
            set 
            { 
                txtFormulaConstants.Text = "";
                foreach (KeyValuePair<string, string> pair in value)
                    txtFormulaConstants.Text += String.Format("{0} = {1}\r\n", pair.Key, pair.Value);
            }
        }

        public PropertiesForm()
        {
            InitializeComponent();
        }

        private void btnFormula_Click(object sender, EventArgs e)
        {
            using (FormulaForm form = new FormulaForm())
            {
                form.VariableXEnabled = false;
                form.FormulaConstants = FormulaConstants;
                if (tabConstants.Focused)
                    form.Formula = txtFormulaConstants.SelectedText;
                else if (tabInformation.Focused)
                    form.Formula = FormulaAlpha;
                else if (tabDetectors.Focused)
                    form.Formula = txtFormulaDetectors.SelectedText;

                if (form.ShowDialog(this) == DialogResult.OK)
                {
                    if (tabConstants.Focused)
                        txtFormulaConstants.SelectedText = form.Formula;
                    else if (tabInformation.Focused)
                        FormulaAlpha = form.Formula;
                    else if (tabDetectors.Focused)
                        txtFormulaDetectors.SelectedText = form.Formula;
                    FormulaConstants = form.FormulaConstants;
                }
            }
        }
    }
}
